package leetcode.editor.data;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        HeroNode node1 = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");
        HeroNode node5 = new HeroNode(5, "关胜");
        //手动创建二叉树
        binaryTree.setRoot(node1);
        node1.left = node2;
        node1.right = node3;
        node3.right = node4;
        node3.left = node5;

//        binaryTree.preOrder();
//        System.out.println("中序遍历");
//        binaryTree.midOrder();
//        System.out.println("后序遍历");
//        binaryTree.postOrder();
//
//        System.out.println("找到了");
//        HeroNode heroNode = binaryTree.preQuery(5);
//        System.out.println(heroNode);
//
//        System.out.println("中序查找");
//        HeroNode heroNode1 = binaryTree.midQuery(4);
//        System.out.println(heroNode1);
//
//        System.out.println("后续查找");
//        HeroNode heroNode2 = binaryTree.postQuery(5);
//        System.out.println(heroNode2);

        System.out.println("测试删除节点");
        binaryTree.postOrder();
        binaryTree.deleteNode(5);
        System.out.println("删除后");
        binaryTree.postOrder();
    }
}
class BinaryTree {
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public void deleteNode (int no) {
        if (this.root!=null) {
            if (root.no==no) {
                this.root =null;
            }
            this.root.deleteNode(no);
        }else {
            System.out.println("空树不能删除");
        }
    }
//    前序遍历
    public void preOrder() {
        if (this.root!=null) {
            this.root.preOrder();
        }else {
            System.out.println("当前二叉树为空");
        }
    }

    public HeroNode preQuery(int no) {
        if (this.root!=null) {
            return this.root.preQuery(no);
        }
        return null;
    }

    public void midOrder() {
        if (this.root!=null) {
            this.root.midOrder();
        }else {
            System.out.println("当前二叉树为空");
        }
    }
    public HeroNode midQuery(int no) {
        if (this.root!=null) {
            return this.root.midQuery(no);
        }
        return null;
    }

    public void postOrder() {
        if (this.root!=null) {
            this.root.postOrder();
        }else {
            System.out.println("当前二叉树为空");
        }
    }

    public HeroNode postQuery(int no) {
        if (this.root!=null) {
            return this.root.postQuery(no);
        }
        return null;
    }
}

class HeroNode {
    public int no;
    public String name;
    public HeroNode left;
    public HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    /**
     * 若是叶子节点直接删除  非叶子节点首先让左子节点代替,没有则让右叶子节点代替
     * @param no
     */
    public void deleteNode2(int no) {
        if (this.no==no) {

        }
    }

    /**
     * 若是叶子节点直接删除  非叶子节点直接删除子树
     */
    public void deleteNode(int no) {
        if (this.left!=null&&this.left.no==no) {
            this.left = null;
            return;
        }
        if (this.right!=null&&this.right.no==no) {
            this.right = null;
            return;
        }
//        递归
        if (this.left!=null) {
            this.left.deleteNode(no);
        }

        if (this.right!=null) {
            this.right.deleteNode(no);
        }
    }

    //编写前序遍历方法
    public void preOrder() {
        System.out.println(this);  //输出当前节点
        //递归左子树
        if (this.left!=null) {
            this.left.preOrder();
        }
        if (this.right!=null) {
            this.right.preOrder();
        }
    }
    //前序查找
    public HeroNode preQuery(int no) {
        if (this.no == no) {
            return this;
        }
        HeroNode heroNode =null;
        if (this.left!=null) {
            heroNode = this.left.preQuery(no);
        }
        if (heroNode!=null) {
            //说明左边找到
            return heroNode;
        }
        if (this.right!=null) {
            heroNode = this.right.preQuery(no);
        }
        //不管找到没找到都需要返回了
        return heroNode;
    }

    //编写中序遍历方法
    public void midOrder() {
        //递归左子树
        if (this.left!=null) {
            this.left.midOrder();
        }
        System.out.println(this);  //输出当前节点
        if (this.right!=null) {
            this.right.midOrder();
        }
    }

//    中序查找
    public HeroNode midQuery(int no) {
        HeroNode heroNode = null;
        if (this.left!=null) {
            heroNode = this.left.midQuery(no);
        }
        if (heroNode !=null) {
            return heroNode;
        }
        if (this.no == no) {
            return this;
        }
        //
        if (this.right!=null) {
            heroNode = this.right.midQuery(no);
        }
        return heroNode;
    }

    //编写后序遍历方法
    public void postOrder() {
        //递归左子树
        if (this.left!=null) {
            this.left.postOrder();
        }
        if (this.right!=null) {
            this.right.postOrder();
        }
        System.out.println(this);  //输出当前节点
    }


    public HeroNode postQuery(int no) {
        HeroNode heroNode = null;
        if (this.left!=null) {
            heroNode = this.left.postQuery(no);
        }
        if (heroNode !=null) {
            return heroNode;
        }
        if (this.right!=null) {
            heroNode = this.right.postQuery(no);
        }
        if (this.no == no) {
            return this;
        }
        return heroNode;
    }

}
